<template>
  <el-dialog
    :title="title"
    :width="width"
    top="50px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="isVisible"
  >
    <el-form
      ref="classObj"
      :model="classObj"
      :rules="rules"
      label-position="right"
      label-width="100px"
    >
      <el-form-item :label="'图书名称'" prop="className">
        <el-input v-model="classObj.className" />
      </el-form-item>
      <el-form-item align="center" label="图书摘要" prop="pclassId">
        <el-input v-model="classObj.pclassId" type="textarea" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        type="warning"
        plain
        :loading="buttonLoading"
        @click="isVisible = false"
      >
        {{ $t("common.cancel") }}
      </el-button>
      <el-button
        type="primary"
        plain
        :loading="buttonLoading"
        @click="submitForm"
      >
        {{ $t("common.confirm") }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { validMobile } from '@/utils/my-validate'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'ClassEdit',
  components: { Treeselect },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      classObj: this.initClass(),
      buttonLoading: false,
      screenWidth: 0,
      width: this.initWidth(),
      rules: {
        className: { required: true, message: this.$t('rules.require'), trigger: 'blur' },
        PClassId: { required: true, message: this.$t('rules.require'), trigger: 'blur' }
      }
    }
  },
  computed: {
    isVisible: {
      get() {
        return this.dialogVisible
      },
      set() {
        this.close()
        this.reset()
      }
    }
  },
  mounted() {
    window.onresize = () => {
      return (() => {
        this.width = this.initWidth()
      })()
    }
  },
  methods: {
    initClass() {
      return {
        className: '',
        PClassId: ''
      }
    },
    initWidth() {
      this.screenWidth = document.body.clientWidth
      if (this.screenWidth < 991) {
        return '90%'
      } else if (this.screenWidth < 1400) {
        return '45%'
      } else {
        return '800px'
      }
    },
    setBook(val) {
      console.log(val)
      this.classObj = { ...val }
    },
    close() {
      this.$emit('close')
    },
    submitForm() {
      this.classObj.PClassId = this.classObj.pclassId
      this.$refs.classObj.validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (!this.classObj.classId) {
            // create
            this.$post('study/lClassInfo', { ...this.classObj }).then(() => {
              this.buttonLoading = false
              this.isVisible = false
              this.$message({
                message: this.$t('tips.createSuccess'),
                type: 'success'
              })
              this.$emit('success')
            })
          } else {
            // update
            this.$put('study/lClassInfo', { ...this.classObj }).then(() => {
              this.buttonLoading = false
              this.isVisible = false
              this.$message({
                message: this.$t('tips.updateSuccess'),
                type: 'success'
              })
              this.$emit('success')
            })
          }
        } else {
          return false
        }
      })
    },
    reset() {
      // 先清除校验，再清除表单，不然有奇怪的bug
      this.$refs.classObj.clearValidate()
      this.$refs.classObj.resetFields()
      this.classObj = this.initClass()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
